using System;
using System.Collections.Generic;
using System.Text;

using System.Xml;

namespace Winsmarts.PI.Common
{
    public class Mappings : IDictionary<string, Mapping>
    {
        private Dictionary<string, Mapping> localData = new Dictionary<string, Mapping>();
        private string accountField = "AccountName";

        public string AccountField
        {
            get { return accountField; }
        }

        public Mapping this[string inputField]
        {
            get 
            {
                return localData[inputField];
            }
            set
            {
                if (!localData.ContainsKey(value.InputField))
                {
                    localData.Add(value.InputField, value);
                }
                else
                {
                    Trace.WriteLine("Duplicate entry found for mapping: " + value.InputField + ". Ignoring the second and subsequent.");
                }
            }
        }


        #region IDictionary<string,Mapping> Members

        public void Add(string key, Mapping value)
        {
            localData.Add(key, value);
        }

        public bool ContainsKey(string key)
        {
            return localData.ContainsKey(key);
        }

        public ICollection<string> Keys
        {
            get { return localData.Keys; }
        }

        public bool Remove(string key)
        {
            return localData.Remove(key);
        }

        public bool TryGetValue(string key, out Mapping value)
        {
            return localData.TryGetValue(key, out value);
        }

        public ICollection<Mapping> Values
        {
            get { return localData.Values ; }
        }

        #endregion

        #region ICollection<KeyValuePair<string,Mapping>> Members

        public void Add(KeyValuePair<string, Mapping> item)
        {
            localData.Add(item.Key, item.Value);
        }

        public void Clear()
        {
            localData.Clear();
        }

        public bool Contains(KeyValuePair<string, Mapping> item)
        {
            return localData.ContainsKey(item.Key) & localData.ContainsValue(item.Value);
        }

        public void CopyTo(KeyValuePair<string, Mapping>[] array, int arrayIndex)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public int Count
        {
            get { return localData.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(KeyValuePair<string, Mapping> item)
        {
            return localData.Remove(item.Key);
        }

        #endregion

        #region IEnumerable<KeyValuePair<string,Mapping>> Members

        public IEnumerator<KeyValuePair<string, Mapping>> GetEnumerator()
        {
            return localData.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return localData.GetEnumerator();
        }

        #endregion

        public Mappings(string mappingFile)
        {
            XmlDocument mapDoc = new XmlDocument();
            mapDoc.Load(mappingFile);
            XmlNodeList mappingNodes = mapDoc.SelectNodes("./Mappings/Mapping");
            string accountField = "AccountName";
            foreach (XmlNode mappingNode in mappingNodes)
            {
                Mapping newMapping = new Mapping(mappingNode);
                this[newMapping.InputField] = newMapping;
                if (newMapping.MOSSField == "AccountName")
                    accountField = newMapping.InputField;
            }
        }
    }
}
